<template>
  <q-page class="q-pa-md">
    <div class="row items-center">
      <div class="q-pb-md text-h5">
        {{ pageTitle }}
        <span class="text-subtitle1">列表</span>
      </div>
      <q-space></q-space>
      <search-bar-toggle-btn v-model="searchBarVisibility"></search-bar-toggle-btn>
    </div>
    <div class="q-pa-sm shadow-2 q-mb-md items-center" v-show="searchBarVisibility">
      <div class="row q-col-gutter-sm">
        <div class="col-12 col-sm-6 col-md-4 col-lg-3 row q-col-gutter-sm">
          <div class="col-4 text-right searchbar-label">bill of lading</div>
          <div class="col-8">
            <easy-input v-model="fpsReq.billOfLading" @focus="fx.ui.select" @keyup.enter="loadListData(2)"
              :readonly="listDataLoadingState">
            </easy-input>
          </div>
        </div>
        <div class="col-12 col-sm-6 col-md-4 col-lg-3 row q-col-gutter-sm">
          <div class="col-4 text-right searchbar-label">创建日期从</div>
          <div class="col-8">
            <easy-input v-model="fpsReq.createDtBeg" use-date-picker append-icon-show :readonly="listDataLoadingState" />
          </div>
        </div>
        <div class="col-12 col-sm-6 col-md-4 col-lg-3 row q-col-gutter-sm">
          <div class="col-4 text-right searchbar-label">创建日期至</div>
          <div class="col-8">
            <easy-input v-model="fpsReq.createDtEnd" use-date-picker append-icon-show :readonly="listDataLoadingState" />
          </div>
        </div>
        <div class="col-shrink row q-col-gutter-sm">
          <div class="col">
            <q-toggle v-model="fpsReq.isSuccess" :disable="listDataLoadingState" @update:model-value="loadListData(2)"
              dense toggle-indeterminate class="q-ma-xs" label="是否成功"/>
          </div>
        </div>
      </div>
      <div class="row q-pt-sm">
        <div class="q-mr-sm">
          <q-btn :disable="listDataLoadingState" @click="loadListData(2)" color="primary" icon="search" label="查询" />
        </div>
      </div>
    </div>
    <div class="shadow-2">
      <q-table @request="fpsAssist.onDataTableRequest" :rows="$d.tableData" :columns="fpsInfo.columnsInfo"
        separator="cell" row-key="id" v-model:pagination="fpsInfo.paginationInfo" :visible-columns="fpsInfo.visibleColumns"
        dense flat :hide-pagination="true" :loading="listDataLoadingState" color="primary" binary-state-sort hide-selected-banner
        selection="single" v-model:selected="$d.selectedItems"
        @row-dblclick="(evt,row)=>{fpsNav.gotoDetail(row.id)}">
        <template v-slot:top="props">
          <q-btn :disable="$d.selectedItems.length !== 1" @click="downloadListExcel" color="primary" icon="save_alt"
            label="导出Excel"/>
          <!--填充所有剩余空间-->
          <q-space />
          <q-btn @click="downloadListExcel" dense flat icon="save_alt" title="导出Excel" />
          <table-col-options-cfg v-model:table-columns="fpsInfo.columnsInfo"
            v-model:visible-columns="fpsInfo.visibleColumns" class="q-mx-sm"
            @update-col-options-for-save="fpsAssist.saveColOptions">
          </table-col-options-cfg>
          <q-btn flat dense :icon="props.inFullscreen ? 'fullscreen_exit' : 'fullscreen'"
            :title="props.inFullscreen ? '退出全屏' : '全屏显示'" @click="props.toggleFullscreen"
            :ripple="false" />
        </template>
        <!--操作列插槽-->
        <template v-slot:body-cell-operation="props">
          <q-td :props="props" auto-width>
            <q-btn color="primary" label="查看" class="q-mr-xs btn-in-row" :dense="true"
              @click="evt => fpsNav.gotoDetail(props.row.id, evt)" />
          </q-td>
        </template>
        <template v-slot:body-cell-isSuccess="props">
          <q-td :props="props" auto-width>
            <q-badge :color="props.row.isSuccess?'positive':'grey-6'"
              :label="props.row.isSuccess ? '是' : '否'" />
          </q-td>
        </template>
        <template v-slot:no-data>
          <div class="full-width items-center justify-center row">
            <q-icon name="error_outline" />
            无数据
            <q-btn flat icon="refresh" :loading="listDataLoadingState"
              :disable="listDataLoadingState" dense title="刷新" size="sm" class="q-ml-sm" @click="loadListData"></q-btn>
          </div>
        </template>
        <template v-slot:bottom>
          <paging-bar :pageNo="fpsReq.pageNo" :pageSize="fpsReq.pageSize" :totalAmount="fpsInfo.totalAmount"
            @pagesize-changed="fpsAssist.onPageSizeChanged" @go-to-page="fpsNav.gotoPage"
            :loading-state="listDataLoadingState" />
        </template>
      </q-table>
    </div>
  </q-page>
</template>

<script lang="ts">
export default {
  name: 'DecBackFillList'
}
</script>

<script lang="ts" setup>

import {useTitleHelper} from '@njt/foundation/hooks/titleHelper'
import {reactive, ref} from 'vue'
import {ColumnInfo} from '@njt/foundation/defs/comm'
import {useFpsHelper} from '@njt/foundation/hooks/fpsHelper'
import {DecBackFillInfo, GetDecBackFillListReq, GetDecBackFillListResp} from 'src/defs/ims/decBackFill'
import dayjs from 'dayjs'

const pageTitle = '报关单发票反填'
useTitleHelper(pageTitle)

const $d = reactive({
  tableData: [] as DecBackFillInfo[],
  selectedItems: [] as DecBackFillInfo[]
})

let fpsReq: GetDecBackFillListReq = reactive({
  billOfLading: null,
  createDtBeg: null,
  createDtEnd: null,
  isSuccess: null,
  pageNo: 1,
  pageSize: 40,
  isDescending: false,
  orderBy: null
})

const columnsDefine: ColumnInfo[] = [
  {
    label: '操作',
    name: 'operation',
    field: 'operation',
    align: 'left',
    defVisible: true,
    defOrder: 1,
  },
  {
    label: '序号',
    name: 'id',
    field: 'id',
    align: 'left',
    defVisible: true,
    sortable: true,
    defOrder: 2,
  },
  {
    label: 'bill of lading',
    name: 'billOfLading',
    field: 'billOfLading',
    align: 'left',
    defVisible: true,
    defOrder: 3,
  },
  {
    label: '是否成功',
    name: 'isSuccess',
    field: 'isSuccess',
    align: 'left',
    defVisible: true,
    defOrder: 5,
  },
  {
    label: '错误信息',
    name: 'backMsg',
    field: 'backMsg',
    align: 'left',
    defVisible: true,
    defOrder: 6,
    format: (val: string | null) => {
      return (val && val.length>130)?`${val.slice(0, 129)}...`:val
    }
  },
  {
    label: '创建人',
    name: 'createP',
    field: 'createP',
    align: 'left',
    defVisible: true,
    defOrder: 7,
    format: (val: string,row: DecBackFillInfo) => $f.fmt.userDisp(val, row.createPn)
  },
  {
    label: '创建时间',
    name: 'createDt',
    field: 'createDt',
    align: 'left',
    defVisible: true,
    defOrder: 8,
    format: (val: string) => $f.fmt.dtFmtFullNoSec(val)
  },
]

let listDataLoadingState = ref(false)
const loadListData = function (resetFpsReq?: number) {
  if (resetFpsReq) {
    if (resetFpsReq >= 1) {
      fpsReq.pageNo = 1
    }
    if (resetFpsReq >= 2) {
      fpsReq.orderBy = ''
      fpsReq.isDescending = false
    }
  }
  listDataLoadingState.value = true
  $f.axios
    .get<GetDecBackFillListResp>('/mhlbc/ims/get-dec-back-fill-list', {params: fpsReq})
    .then((resp) => {
      if (resp.data.createDtBeg)
        resp.data.createDtBeg = $f.fmt.dtFmtDateOnly(resp.data.createDtBeg)
      if (resp.data.createDtEnd)
        resp.data.createDtEnd = $f.fmt.dtFmtDateOnly(resp.data.createDtEnd)
      fpsAssist.handleGenericFpsResp(resp.data)
      $d.tableData = resp.data.decBackFillInfos
      fpsAssist.saveFpsReq(fpsReq)
    })
    .catch((err) => {
      $f.ui.showNotifyFailed($f.getAxiosErrMsg(err))
    })
    .finally(() => {
      listDataLoadingState.value = false
    })
}

const {
  fpsInfo,
  fpsNav,
  fpsAssist,
  searchBarVisibility
} = useFpsHelper('/ims/dec-back-fill',
  '/ims/dec-back-fill/detail',
  null,
  null,
  fpsReq,columnsDefine,
  null,
  loadListData
)

const downloadListExcel = function () {
  let timeStamp = dayjs()
  let formattedString = dayjs(timeStamp).format('YYYYMMDDHHmmss')
  let fileName = 'DecBack_' + formattedString + '.xlsx'
  let params = $f.misc.makeSimpleQueryObj(fpsReq, [], { pageNo: 1, pageSize: 0x7fffffff })
  if ($d.selectedItems.length) {
    params['ids'] = [$d.selectedItems[0].id]
  }
  let url = $f.misc.makeUrlWithQueryParam(
      '/mhlbc/ims/get-dec-back-fill-report-excel',
      params
  )
  $f.nw.downloadFileWithAuth(url, fileName, false)
  $d.selectedItems = []
}

const fx = $f
</script>

<style scoped>

</style>
